/**
 * Created by JasonChou on 17/7/7.
 */
const http = require('./libs/http');
const config = require('./config');
const companyId = config.companyId;

const limit = 10;
module.exports = {
    getCompanyDetail() {
        return http.get({
            url: '/new/h5/company', data: {companyId}
        }).then(res => {

            const pics = [];

            const {environment, topBanner, hot} = res.data.data.company;
            res.data.data.company.hot = Math.floor(hot);

            if (topBanner && topBanner.length)
                topBanner[0] = topBanner[0] + '?imageView2/2/w/600'; //背景
            res.data.data.company.logo = res.data.data.company.logo + '?imageView2/1/w/300/h/300';

            environment.map((pic, i) => {
                if (i < 4)
                    pics.push(pic + '?imageView2/1/w/300/h/300');//首页


                environment[i] = {
                    pic: pic + '?imageView2/2/w/400/',//瀑布流列表
                    raw: pic + '?imageView2/2/w/700/' //瀑布流详情
                }
            });

            res.data.data.company.pics = pics;
            res.data.data.environment = environment;


            res.data.data.clerks.map(clerk => {
                clerk.avatar = clerk.avatar + '?imageView2/1/w/200/h/200';

                clerk.products.map(product => {
                    product.images[0] = product.images[0] + '?imageView2/1/w/300/h/300';
                })
            });

            res.data.data.sales.map(sale => {
                sale.image_urls[0] = sale.image_urls[0] + '?imageView2/1/w/300/h/300';
            });

            return res;
        })
    },

    submitOrder(phone, type) {
        return http.post({url: '/new/company/customer/leaveContact', data: {companyId, type, phone}})
    },

    getClerks(index = 0) {
        return http.get({url: '/new/h5/company/clerks/list', data: {companyId, limit, index}})
    },

    getSales(index = 0) {
        return http.get({url: '/new/h5/company/sales/list', data: {companyId, limit, index}})
    },

    getTattooer(userId) {
        return http.get({url: '/new/users', data: {userId}}).then(res => {
            res.data.data.hot = Math.floor(res.data.data.hot);
            return res;
        })
    },

    getProducts(userId, updated) {
        return http.get({url: '/new/products', data: {userId, updated, limit}}).then(res => {
            if (!res.data.data)
                res.data.data = [];
            return res;
        })
    }
};